'use strict';

stareal
    .controller("createViewHtmlController", function ($scope,$http,$stateParams, $lazyLoader, $api, $alert, $state,localStorageService,$window,$compile,FileUploader) {
        // ue.getContent(), 获取
        //  ue.execCommand('cleardoc'); //清除
        // ue.ready(function () {
        /*console.log("${contend}");  编辑
        ue.setContent(contend);
    });*/
           UE.delEditor('detail');
        var ue = UE.getEditor('detail');
        $scope.viewID=$stateParams.id;
        $("#detail").height(window.screen.height -200);

        $scope.getDetailContent=function(){
            $api.get("app/view/caoretrievelist",{id:$scope.viewID}, true)
                .then(function(ret) {
                    // 生成编辑器
                    // var editor = new ___E('vieweditortextarea1');
                    //
                    // // 自定义配置
                    // editor.config.uploadImgUrl =url;
                    //
                    // // editor.config.menus = ['bold', 'quote', 'list','img'];
                    //
                    // // 初始化
                    // editor.init();


                    // $scope.$txt = editor.$txt;
                    // UE.delEditor('detail');
                    // $scope.ue = UE.getEditor('detail');
                    var sContent = ret.data;
                    // console.log(sContent);
                    var newCOntents='';
                    if(sContent.type!=1){
                        var arrContent=JSON.parse(sContent.content);
                        for (var i = 0; i < arrContent.length; i++) {
                            if(arrContent[i].type == 'title'){
                                newCOntents +='<p style="font-weight:bold;font-size: 16px;">'+arrContent[i].title+'</p>';
                            }else if(arrContent[i].type == 'img'){
                                newCOntents +='<img src="'+arrContent[i].img+'">';
                            }else if(arrContent[i].type == 'text'){
                                newCOntents +='<p>'+arrContent[i].text+'</p>';
                            }
                        }
                        sContent.content=newCOntents;
                    }
                    $scope.sContent = sContent;
                    if( $scope.sContent.content!=undefined&& $scope.sContent.content!=null&& $scope.sContent.content!=''){
                        ue.ready(function() {
                            ue.setContent($scope.sContent.content);
                        });

                    }

                   // editor.txt.html($scope.sContent.content);
                    //$scope.$txt.html($scope.sContent.content);
                    //  var classifyliId=sContent.classifyid; //分类id
                    //  var useId=sContent.user_id;

                })
        }
        $scope.getDetailContent();
        //下一步
        $scope.nextGo=function(){
            var html =ue.getContent();
            var text = ue.getContentTxt();
            if(text!=''){

                // console.log(html);
                localStorageService.set("TopicViewdescriptions",text.slice(0,200));
                localStorageService.set("TopicViewcontents",html);
                localStorageService.set("TopicViewtextlength",text.length);
                $state.go("main.sendView",{id:$scope.viewID},true);
            }else{
                $alert.show('发表内容不能为空!');
            }
        }
        //获取内容
        $scope.getContent=function(){
            var content= ue.getContent();
            var textlength=0;//文字个数
            var imglength=0;//图片张数
            $scope.description=ue.getContentTxt().slice(0,200);
            $scope.topic_title=localStorageService.get("TopicViewname");
            $scope.position= localStorageService.get("currentPOSITIONID");
            $scope.contents=content;
            // $scope.imglength=imglength;
            // $scope.textlength=textlength;

            // $scope.lengthtime=$('#viewclassifytimes').val();
            return true;


        }
        //保存草稿
        $scope.SaveCaoGaoView=function() {
            if ($scope.getContent()) {
                var token = localStorageService.get('token');
                //var url = 'https://api.mydeershow.com/mobile/app/view/create?source=pc&accessToken='+token;//正式
                //var url = 'http://192.168.1.3/mobile/app/view/create?source=pc&accessToken=' + token;//测试
                var url='https://test.djx.api02.opentickets.cn/mobile/app/view/create?source=pc&accessToken=' + token;//测试
                $http({
                    method: 'post',
                    url: url,
                    data: {
                        id: $scope.viewID,
                        name: $scope.topic_title,
                        thumb: '',
                        content: $scope.contents,
                        describes: $scope.description,
                        position: $scope.position,
                        status: -1,
                        type:1, //htmls编辑器
                    },
                    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                    transformRequest: function (data) {
                        return $.param(data);
                    }
                }).then(function successCallback(response) {
                    // $state.go('my.view',{},true);
                    $alert.show("保存成功");
                    // $state.reload();
                }, function errorCallback(response) {
                    $alert.show("保存失败");
                });


            }
        }
    })